﻿// Inner Fire 游戏引擎库
// 渲染项目类-UI渲染项目
//
// Copyright (c) 2024 by 尔西大帝. All rights reserved.
//
// 用于UI渲染项目
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-12-21

#include "ui_render_item.h"
#include <dx/ui/ui_canvas.h>

namespace ifire::dx {
UIRenderItem::UIRenderItem(const UIWidget& widget, size_t capacity)
    : mCapacity(capacity)
    , mWidget(widget)
    , mPoolIndex(0)
    , mParent(nullptr)
    , mCanvas(nullptr)
    , mVisible(true) {}

UIWidget UIRenderItem::GetParentWidget() {
  // 如果项目为空，则设置Canvas的Widget
  if (mParent == nullptr) {
    return mCanvas->mWidget;
  }

  // 如果拥有父级，得到父级计算之后的Widget
  return mParent->CalcWidget();
}

UIWidget UIRenderItem::CalcWidget() {
  // 得到父级的Widget
  UIWidget parent_widget = GetParentWidget();
  // 计算自已的Widget
  return mWidget.CalcWidget(parent_widget);
}

void UIRenderItem::SetVisible(bool state) {
  if (mVisible != state) {
    mVisible = state;
    SetRefresh();
  }
}

void UIRenderItem::SetScale(XMFLOAT2 scale) {
  mWidget.scale = scale;
  SetRefresh();
}

} // namespace ifire::dx